using CommunityToolkit.Mvvm.Input;
using DialogHostAvalonia;

namespace SchoolApp_Material.ViewModels;

public class MsgBoxViewModel : ViewModelBase
{
    public MsgBoxViewModel()
    {
    }

    #region 公开属性

    private MsgBoxIcons _msgBoxIcon;

    public MsgBoxIcons MsgBoxIcon
    {
        get => _msgBoxIcon;
        set
        {
            SetProperty(ref _msgBoxIcon, value);
            switch (MsgBoxIcon)
            {
                case MsgBoxIcons.Information:
                    Icon = "Information";
                    IconVisible = true;
                    break;
                case MsgBoxIcons.Warning:
                    Icon = "Alert";
                    IconVisible = true;
                    break;
                case MsgBoxIcons.Error:
                    Icon = "CloseCircle";
                    IconVisible = true;
                    break;
                case MsgBoxIcons.None:
                    Icon = "Infinity";
                    IconVisible = false;
                    break;
            }
        }
    }

    private MsgBoxShowButtons _msgBoxShowButton;

    public MsgBoxShowButtons MsgBoxShowButton
    {
        get => _msgBoxShowButton;
        set
        {
            SetProperty(ref _msgBoxShowButton, value);
            switch (MsgBoxShowButton)
            {
                case MsgBoxShowButtons.Ok:
                    OkVisible = true;
                    CancelVisible = false;
                    YesVisible = false;
                    NoVisible = false;
                    break;
                case MsgBoxShowButtons.Cancel:
                    OkVisible = false;
                    CancelVisible = true;
                    YesVisible = false;
                    NoVisible = false;
                    break;
                case MsgBoxShowButtons.Yes:
                    OkVisible = false;
                    CancelVisible = false;
                    YesVisible = true;
                    NoVisible = false;
                    break;
                case MsgBoxShowButtons.No:
                    OkVisible = false;
                    CancelVisible = false;
                    YesVisible = false;
                    NoVisible = true;
                    break;
                case MsgBoxShowButtons.OkCancel:
                    OkVisible = true;
                    CancelVisible = true;
                    YesVisible = false;
                    NoVisible = false;
                    break;
                case MsgBoxShowButtons.YesNo:
                    OkVisible = false;
                    CancelVisible = false;
                    YesVisible = true;
                    NoVisible = true;
                    break;
                case MsgBoxShowButtons.YesNoCancel:
                    OkVisible = false;
                    CancelVisible = true;
                    YesVisible = true;
                    NoVisible = true;
                    break;
            }
        }
    }

    private string _title;

    public string Title
    {
        get => _title;
        set => SetProperty(ref _title, value);
    }

    private string _content;

    public string Content
    {
        get => _content;
        set => SetProperty(ref _content, value);
    }

    private MsgBoxButtons? _defaultEnter;

    public MsgBoxButtons? DefaultEnter
    {
        get => _defaultEnter;
        set => SetProperty(ref _defaultEnter, value);
    }

    private MsgBoxButtons? _defaultEscape;

    public MsgBoxButtons? DefaultEscape
    {
        get => _defaultEscape;
        set => SetProperty(ref _defaultEscape, value);
    }

    #endregion

    #region 私有属性

    private string _icon;

    public string Icon
    {
        get => _icon;
        private set => SetProperty(ref _icon, value);
    }

    private bool _iconVisible;

    public bool IconVisible
    {
        get => _iconVisible;
        private set => SetProperty(ref _iconVisible, value);
    }

    private bool _okVisible;

    public bool OkVisible
    {
        get => _okVisible;
        private set => SetProperty(ref _okVisible, value);
    }

    private bool _cancelVisible;

    public bool CancelVisible
    {
        get => _cancelVisible;
        private set => SetProperty(ref _cancelVisible, value);
    }

    private bool _yesVisible;

    public bool YesVisible
    {
        get => _yesVisible;
        private set => SetProperty(ref _yesVisible, value);
    }

    private bool _noVisible;

    public bool NoVisible
    {
        get => _noVisible;
        private set => SetProperty(ref _noVisible, value);
    }

    #endregion

    #region 命令

    private RelayCommand _okCmd;

    public RelayCommand OkCmd =>
        _okCmd ??= new RelayCommand(ExecuteOkCmd);

    private void ExecuteOkCmd()
    {
        DialogHost.GetDialogSession(null)?.Close(MsgBoxButtons.Ok);
    }

    private RelayCommand _cancelCmd;

    public RelayCommand CancelCmd =>
        _cancelCmd ??= new RelayCommand(ExecuteCancelCmd);

    private void ExecuteCancelCmd()
    {
        DialogHost.GetDialogSession(null)?.Close(MsgBoxButtons.Cancel);
    }

    private RelayCommand _yesCmd;

    public RelayCommand YesCmd =>
        _yesCmd ??= new RelayCommand(ExecuteYesCmd);

    private void ExecuteYesCmd()
    {
        DialogHost.GetDialogSession(null)?.Close(MsgBoxButtons.Yes);
    }

    private RelayCommand _noCmd;

    public RelayCommand NoCmd =>
        _noCmd ??= new RelayCommand(ExecuteNoCmd);

    private void ExecuteNoCmd()
    {
        DialogHost.GetDialogSession(null)?.Close(MsgBoxButtons.No);
    }

    private RelayCommand _defaultEnterCmd;

    public RelayCommand DefaultEnterCmd =>
        _defaultEnterCmd ??= new RelayCommand(ExecuteDefaultEnterCmd);

    private void ExecuteDefaultEnterCmd()
    {
        if (DefaultEnter.HasValue)
            DialogHost.GetDialogSession(null)?.Close(DefaultEnter);
    }

    private RelayCommand _defaultEscapeCmd;

    public RelayCommand DefaultEscapeCmd =>
        _defaultEscapeCmd ??= new RelayCommand(ExecuteDefaultEscapeCmd);

    private void ExecuteDefaultEscapeCmd()
    {
        if (DefaultEscape.HasValue)
            DialogHost.GetDialogSession(null)?.Close(DefaultEscape);
    }

    #endregion
}

/// <summary>
/// 消息框显示按钮
/// </summary>
public enum MsgBoxShowButtons
{
    Ok,
    Cancel,
    Yes,
    No,
    OkCancel,
    YesNoCancel,
    YesNo
}

/// <summary>
/// 消息框图标
/// </summary>
public enum MsgBoxIcons
{
    Information,
    Warning,
    Error,
    None
}

/// <summary>
/// 消息框按钮
/// </summary>
public enum MsgBoxButtons
{
    Ok,
    Cancel,
    Yes,
    No,
}